<template>
    <div>
        <el-dialog :title="typeName" :visible.sync="dialogVisible" width="60%" center>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
                <el-form-item label="收货人" prop="name">
                    <el-input v-model="ruleForm.name" minlength="2" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="收货电话" prop="tel">
                    <el-input v-model="ruleForm.tel"></el-input>
                </el-form-item>
                <el-form-item label="省市区县" prop="area">
                    <el-row>
                        <el-col :span="8">
                            <el-select v-model="ruleForm.province" placeholder="请选择省">
                                <el-option v-for="item in options" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                        <el-col :span="8">
                            <div class="ml-15 mr-15">
                                <el-select v-model="ruleForm.city" placeholder="请选择市">
                                    <el-option v-for="item in options" :key="item.value" :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-select v-model="ruleForm.area" placeholder="请选择区">
                                <el-option v-for="item in options" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                    </el-row>
                </el-form-item>

                <el-form-item label="详细地址" prop="address">
                    <el-input v-model="ruleForm.address"></el-input>
                </el-form-item>

                <el-form-item label="默认地址" prop="default">
                    <el-switch v-model="ruleForm.default">
                    </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm('ruleForm')">取 消</el-button>
                <el-button type="danger" @click="submitForm('ruleForm')">新 增</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "evaluateDialog",
    data () {
        return {
            dialogVisible: false,
            typeName: '新增收货地址',
            options: [{
                value: '北京',
                label: '北京'
            }, {
                value: '安徽',
                label: '安徽'
            }, {
                value: '上海',
                label: '上海'
            }],
            ruleForm: {
                name: '',
                tel: '',
                province: '',
                city: '',
                area: '',
                address: '',
                default: false
            },
            rules: {
                name: [
                    { required: true, message: '请输入收货人姓名', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                tel: [
                    { required: true, message: '请输入收货电话', trigger: 'blur' },
                    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
                ],
                province: [
                    { required: true, message: '请选择省', trigger: 'change' }
                ],
                city: [
                    { required: true, message: '请选择市', trigger: 'change' }
                ],
                area: [
                    { required: true, message: '请选择区', trigger: 'change' }
                ],
                address: [
                    { required: true, message: '请输入详细地址', trigger: 'blur' }
                ],

            }
        }
    },
    mounted () {

    },
    methods: {
        handleRemove (file, fileList) {
            console.log(file, fileList)
        },
        handlePictureCardPreview (file) {
            this.dialogImageUrl = file.url
            this.previewDialogVisible = true
        },
        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!')
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        resetForm (formName, ruleForm) {
            this.$refs[formName].resetFields()
            this.dialogVisible = false
        },
        showDialog (type, item) {
            let typeName = '新增收货地址'
            let ruleForm = {
                name: '',
                tel: '',
                province: '',
                city: '',
                area: '',
                address: '',
                default: false
            }
            if (type === 'add') {
                typeName = '新增收货地址'
            } else {
                typeName = '编辑收货地址'
                ruleForm = item
            }

            this.typeName = typeName
            this.dialogVisible = true
            this.ruleForm.name = ruleForm.name
            this.ruleForm.tel = ruleForm.tel
            this.ruleForm.province = ruleForm.province
            this.ruleForm.city = ruleForm.city
            this.ruleForm.area = ruleForm.area
            this.ruleForm.address = ruleForm.address
            this.ruleForm.default = ruleForm.default
        },
    }
}
</script>

<style lang="less" scoped>
.el-select {
    width: 100%;
}
</style>